<?php


include_once("Index.php");


class Welcome extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }


    public function index() {
        $this->initialinfo();
        $this->groupjunp($this->group);
    }


    //登录
    public function loginAction() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $where =array();
        $where['username'] = $username;
        $where['password'] = $password;
        $result = $this->db->select('*')->from('users')->where($where)->get()->result_array();




        if (count($result) != 0) {
            $this->session->username = $result[0]['username'];
            $this->session->islogin = 1;
            $this->session->user_id = $result[0]['id'];
            $arr =array();
            $arr['status'] = 1;
            $arr['message'] = '登录成功';
            $this->setuserinfo($result[0]);
            $this->ajaxResult($arr);
        } else {

            $arr =array();
            $arr['status'] = 0;
            $arr['message'] = '账号或密码错误';
            $this->ajaxResult($arr);
        }
    }

    //登录
    public function login() {

        $this->load->view('index');
    }

    //退出登陆
    public function loginout() {
        $this->session->islogin = 0;
        $this->session->userinfo = "";
        $this->session->user_id = "";
        $this->session->username = "";
        $arr =array();
        $arr['status'] = -1;
        $arr['message'] = '未登录';
        redirect(site_url() . '/Welcome/register');
    }





    //登录
    public function register() {
        if($this->session->islogin ){
            $this->index();
        }

        $this->load->library('parser');
        $data['titel']='登陆';

        $this->load->view('head',$data);
        $this->load->view('login');
    }


    public function sign($key=""){
        if($this->session->islogin ){
            $this->index();
        }
        $data['titel']='注册';
        if(!empty($key)){
          $tuijian =$this->Url2Key($key);
          if(isset($tuijian[1]) ){
              $this->session->recommender_id = $tuijian[1];
          }
        }
        $data['grouplist']=$this->getgrouplist();
        unset($data['grouplist'][0]);
        $this->load->view('head', $data);
        $this->load->view('head',$data);
        $this->load->view('sign');
    }


    /**
     * 完成注册
     */
    public function signaction(){
        $msg['code']=0;
        $row= $this->input->post();

        if(count($row)!=4){
            $msg['code']=1;
            $msg['msg']="参数错误";
            $this->sendjsonmsg($msg);
        }
        foreach ($row as $id=>$v){
            if(empty($v)){
                $msg['code']=1;
                $msg['msg']="缺少参数";
                $this->sendjsonmsg($msg);
            }
            $row[$id] =trim($v);
        }
        if($row['password1']!=$row['password2']){
            $msg['code']=1;
            $msg['msg']="二次密码不一致";
            $this->sendjsonmsg($msg);
        }

        if(strlen($row['phone'])!=11 ){
            $msg['code']=1;
            $msg['msg']="手机格式错误";
            $this->sendjsonmsg($msg);
        }
        $row['group']=2;
       /* if($row['group']<2 ||$row['group']>6){
            $msg['code']=1;
            $msg['msg']="身份错误";
            $this->sendjsonmsg($msg);
        }*/

        if(strlen(intval($row['username']))>15 ){
            $msg['code']=1;
            $msg['msg']="用户名过长";
            $this->sendjsonmsg($msg);
        }


        $this->load->model('Jd_users_model');
        $result=$this->Jd_users_model->GetUserByName($row['username'],"1");
        if(!empty($result)){
            $msg['code']=1;
            $msg['msg']="用户名已经存在";
            $this->sendjsonmsg($msg);
        }

        $result=$this->Jd_users_model->GetUserByPhone($row['phone'],"1");
        if(!empty($result)){
            $msg['code']=1;
            $msg['msg']="手机号已经存在";
            $this->sendjsonmsg($msg);
        }

        $row['password']=$row['password1'];
        unset($row['password1']);
        unset($row['password2']);
        if(!empty( $this->session->recommender_id)){
            $result= $this->Jd_users_model->GetUserById($this->session->recommender_id,"username");
            if(!empty($result)){
                $row['recommender']=$result['username'];
                $row['recommender_id']=$this->session->recommender_id;
                $row['salername']=$result['username'];
            }
        }
        $row['regtime']=time();

        $result= $this->Jd_users_model->adduser($row);
        if($result){
            $msg['msg']="注册成功";
            $this->session->islogin = 1;
            $this->session->user_id =$result ;
            $this->session->username = $row['username'];
            $this->RecommenderTotal($result);//推荐人奖励发放
            $this->sendjsonmsg($msg);
        }
        $msg['code']=1;
        $msg['msg']="注册失败";
        $this->sendjsonmsg($msg);
    }



    //对变量进行json编码
    private function ajaxResult($data) {
        echo json_encode($data);
        die;
    }


    private  function RecommenderTotal($result){
        if($this->session->recommender_id ) {
            $newrow=array();
            $newrow['user_id']=$this->session->recommender_id;
            $newrow['gold']=50;
            $this->moneyupdate($newrow,3,$result);
            $newrow=array();
            $newrow['extension']=50;
            $newrow['extension_nub']=1;
            $newrow['extension_nub_day']=1;
            $newrow['update']=date('Y-m-d');
            $this->UpdataUserinfo($newrow,$this->session->recommender_id);
        }
        $this->session->recommender_id='';
    }



}